import nci60.administration.SQLUploader
import nci60.data.protein.Protein

def sourceFile = new File('/home/auerf/data/IPI/v3.68/ipi.HUMAN.v3.68.dat')


def proteinToId = [:]
Protein.executeQuery('select p.mainId, p.id from Protein p').each{line->
    proteinToId.put(line[0], line[1])
}

def sql = new SQLUploader(new File('/home/auerf/data/NCI60/temp/gene.sql'), 'gene_name', ['name', 'protein_id'])

def currentID = [genes:[]]
BufferedReader file = new BufferedReader(new InputStreamReader(new FileInputStream(sourceFile)));
String line;
while ((line = file.readLine()) != null) {
    if(line.startsWith('//')){
        if(currentID.genes.size()!=0){
//            println 'ID: '+currentID.id
            currentID.genes.each{gene->
                sql << ["'"+gene+"'", proteinToId[currentID.id]]
            }
        }
        currentID = [genes:[]]
    }else if(line.startsWith('ID   ')){
//        println line[5..-1].split('\\.')
        currentID.put('id', line[5..-1].split('\\.')[0])
    }else if(line.startsWith('DR   HGNC')){
        currentID.genes.add(line.split('; ')[2])
    }
}

sql.upload()